//
//  SearchBarTestCell2.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/7/15.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "SearchBarTestCell2.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>

@interface SearchBarTestCell2()

proStrong NSArray *imgUrls;

@end

@implementation SearchBarTestCell2

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addViews];
    }
    return self;
}

- (void)addViews {
    
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.numberOfLines = 0;
    self.nameLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.nameLabel];
    
    self.iconView = [[UIImageView alloc] init];
    [Utils resetView:self.iconView bgColor:nil cornerRadius:10 borderWidth:0 borderColor:nil];
    [self.contentView addSubview:self.iconView];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(10);
        make.right.mas_equalTo(-10);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(10);
        make.bottom.mas_equalTo(-5);
        make.right.mas_equalTo(-10);
        make.height.equalTo(self.iconView.mas_width).multipliedBy(0.5);
    }];
    
    self.imgUrls = @[@"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=366763321,1800567094&fm=26&gp=0.jpg",
    @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594880289623&di=4025d633f08aaccecb5b853070ae5610&imgtype=0&src=http%3A%2F%2Fimg8.zol.com.cn%2Fbbs%2Fupload%2F17982%2F17981927.jpg",
                      @"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1267942788,3228857973&fm=26&gp=0.jpg",
    @"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=297458699,3279797086&fm=26&gp=0.jpg",
                      @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594880344058&di=3b8cc1a067376cdb94016e5500215287&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fphotoblog%2F1402%2F10%2Fc9%2F31147236_1392046318516_mthumb.jpg",
    @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594880344057&di=5a1052f4af772dee69c88cd59690e380&imgtype=0&src=http%3A%2F%2Faliyunzixunbucket.oss-cn-beijing.aliyuncs.com%2Fjpg%2Fbd57395940e68c946be5eb6db75c4f75.jpg%3Fx-oss-process%3Dimage%2Fresize%2Cp_100%2Fauto-orient%2C1%2Fquality%2Cq_90%2Fformat%2Cjpg%2Fwatermark%2Cimage_eXVuY2VzaGk%3D%2Ct_100",
    @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594880344057&di=0cd8ae995354167b3f87cd3a230f1268&imgtype=0&src=http%3A%2F%2Fimages.adsttc.com.qtlcn.com%2Fmedia%2Fimages%2F58a6%2F7d24%2Fe58e%2Fce2d%2F6900%2F0107%2Fnewsletter%2FDSC_7389%25E5%25A4%25A7%25E4%25BB%2599%25E4%25BA%25BA%25E6%258E%258C%25E8%258A%25B1%25E6%2588%25BF%25E5%25A4%259C%25E6%2599%25AF.jpg%3F1487306011"];
}

- (void)loadData:(SBTObjectCellModel *)model {
    
    NSString *string = [NSString stringWithFormat:@"%@: %@",model.object.name,model.object.desc];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, attr.length)];
    [attr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attr.length)];
    [attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, model.object.name.length)];
    self.nameLabel.attributedText = attr;
    
    int r = arc4random()%255;
    int g = arc4random()%255;
    int b = arc4random()%255;
    self.iconView.backgroundColor = RGB_INT(r, g, b);
    
    NSInteger index = arc4random()%self.imgUrls.count;
    NSString *url = self.imgUrls[index];
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:url]];
    
    if (model.cellHeight == 0) {
        //UILayoutFittingCompressedSize  表示返回最小可能的值
        //UILayoutFittingExpandedSize  表示返回最大可能的值
        self.nameLabel.preferredMaxLayoutWidth = kScreenWidth-20;//self.contentView.size.width 用这个得不到准确的值
        
        [self layoutIfNeeded];
        
        //目前发现算出来的不准确
        CGFloat height = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+1;//+1为分割线高度
        
        CGFloat calHeight = 10 + [Utils textSizeOfAttributedAttr:attr width:kScreenWidth-20].height + 10 + (kScreenWidth-20)/2 + 5 + 1;
        
        NSLog(@" --- height:%.2f,calHeight:%.2f",height,calHeight);
       
        model.cellHeight = calHeight;//height;
    }
}

@end
